import java.lang.reflect.Field;

/**
 * @author zhu
 * @date 2024-04-02
 */
public class MainTest {
    public static void main(String[] args){

        Student stu = new Student("张三",19);
        Class<? extends Student> clazz = stu.getClass();
        Field ageField = null;
        try {
            ageField = clazz.getDeclaredField("age");
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
        ageField.setAccessible(true);

        SetAge annotation = ageField.getAnnotation(SetAge.class);
        int age = annotation.value();
        if (stu.getAge() <= 0 || stu.getAge() > 100){
            throw new RuntimeException("非法的年龄");
        }else if (stu.getAge() < age){
            throw new RuntimeException("年龄不能低于18岁");
        }

        System.out.println("学生姓名：" + stu.getName() + "； " + "年龄：" + stu.getAge());
    }
}
